package puppy.demo.tagRanking;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Map.Entry;
import java.util.concurrent.CyclicBarrier;

import util.delicious.ModelGenerator;
import util.nlp.LM;
import util.string.StringAnalysis;
import util.webSearch.TagAggregator;

public class TagRankerSimple {

	/**
	 * @param args
	 */

	private String pathSuffix = "/home/sergio/Dropbox/data/delicious/";

	//private String output = pathSuffix + "model_filtered.bin";

	public LM model = null;



	public TagRankerSimple(String output) {

		model = ModelGenerator.readModel(output);
		model.initUserWeights();

	

	}

	public LinkedList<String> getSuggestions(String query, boolean userWeight) {
		LinkedList<String> list = new LinkedList<String>();

		Hashtable<String, Double> scores = new Hashtable<String, Double>();
	
		// Fetch tags
		TagAggregator aggregator = new TagAggregator();
		aggregator.fetchTags(query, 30, 0);

		Enumeration<String> keys = model.getGraphModel().keys();

		while (keys.hasMoreElements()) {
			String candidate = keys.nextElement();

			// we add query terms as tag candidates

			aggregator.addStringTags(query);

			RankJob job = new RankJob(model, aggregator.set, candidate, scores,
					null, userWeight);

			job.calculateScore();

		}

		Iterator<Entry<String, Double>> iter = util.hashing.Sorting
				.sortHashNumericValuesDouble(scores, false);

		int MAX = 26;
		int i = 1;
		while (iter.hasNext() && i <= MAX) {
			Entry<String, Double> element = iter.next();

			if (!StringAnalysis.isStopWord(element.getKey())) {

				list.add(element.getKey());

				i++;

			}

		}

		
	
		
		return list;

	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String pathSuffix = "/home/sergio/Dropbox/data/delicious/";

		//String output = pathSuffix + "model_filtered.bin";
		
		String output = pathSuffix + "model_filtered_K0.bin";
		LM model = ModelGenerator.readModel(output);

		
		model.initUserWeights();
		
		boolean userWeight = true;

		//System.out.println("Model loaded: " + model.getGraphModel().size());

		Scanner in = new Scanner(System.in);

		System.out.println("Submit query:");
		String word = "";
		while (!word.equals("exit")) {

			System.out.print(">");
			word = in.nextLine();

			if (!word.equals("exit")) {

				// defining variables
				Hashtable<String, Double> scores = new Hashtable<String, Double>();
				RankMerger merger = new RankMerger(scores);

				// Fetch tags
				TagAggregator aggregator = new TagAggregator();
				aggregator.fetchTags(word, 20, 0);

				System.out.println("The following tags were fetched:");
				System.out.println(aggregator.toString());
				System.out.println("Ranking results...");

				// Rank tags

				Enumeration<String> keys = model.getGraphModel().keys();

				while (keys.hasMoreElements()) {
					String candidate = keys.nextElement();

					// we add query terms as tag candidates

					aggregator.addStringTags(word);

					RankJob job = new RankJob(model, aggregator.set, candidate,
							scores, null, userWeight);

					job.calculateScore();

				}

				new Thread(merger).start();
				// end of ranking

			}

		}

	}

}
